// vars/mavenBuild.groovy
 

def call(Map config) {
    // evaluate the body block, and collect configuration into the object
    
    APP_VERSION = "${config.IMG_VERSION}"
    stage('Package') {
        mvnHome = tool 'maven' 
        
        if(config.SNAPSHOT) {  
           echo '构建开发版本'
           APP_VERSION=APP_VERSION + '-SNAPSHOT'           
        } else{
           echo '构建发布版本'  
           APP_VERSION=APP_VERSION + '-SNAPSHOT'         
        }
        
        MVN_OPT = config.DEPLOY ? "install deploy" : ""
        POM_NAME = config.SNAPSHOT ? config.POM : config.RELEASE_POM
        
        
        if(config.SCM){
            checkout scm 
            //checkout(
            //   [$class: 'GitSCM',
            //    branches: scm.branches, 
            //    extensions: [[$class: 'CloneOption', depth: 1, noTags: false, reference: '', shallow: true]],
            //     userRemoteConfigs: scm.userRemoteConfigs
             //   ]
            //)
        }else{
            git branch: "${config.GIT_BRANCH}", credentialsId: 'chendw', url: "${config.GIT_URL}"
        }
        env.JAVA_HOME = '/usr/local/jdk8'

        env.LD_LIBRARY_PATH = '$LD_LIBRARY_PATH:/usr/local/jdk8/jre/lib/amd64/server'
        JAVA_HOME = '/usr/local/jdk8'    

        try {
            sh  """             
                java -version
                echo "JAVA_HOME:${env.JAVA_HOME}"
                echo '更新版本号 APP_VERSION：${APP_VERSION}'
                //${mvnHome}/bin/mvn versions:set -DnewVersion=${APP_VERSION}
         
                echo '提交版本号'
                //${mvnHome}/bin/mvn versions:commit

                
                echo '构建组件'
                //${mvnHome}/bin/mvn -f ${POM_NAME}.xml -Pdocker${config.NODE_TYPE} -U -DskipTests clean package ${MVN_OPT}
                //${mvnHome}/bin/mvn -U -DskipTests clean -f ${POM_NAME}.xml -P docker${config.NODE_TYPE} ${MVN_OPT}
                ${mvnHome}/bin/mvn -f ${POM_NAME}.xml -Pdocker${config.NODE_TYPE} -U -DskipTests clean package ${MVN_OPT}
                
            """
        }
        catch (exc) {
            echo '回滚版本号'
            sh "${mvnHome}/bin/mvn versions:revert "
            throw exc
        }
    }
    
}